// 提交链接 https://leetcode.cn/problems/minimum-number-of-operations-to-make-array-continuous/submissions/559097099/
// 2009. 使数组连续的最少操作数
// 完成日期： 2024.8.30
// c++ 哈希表记录每一个不同的数，双指针枚举范围求需要修改的次数


class Solution {
public:
    map<int, int> ma;
    vector<int> arr;
    int minOperations(vector<int>& nums) {
        for (auto& x : nums)
        {
            if (!ma[x])
            {
                arr.push_back(x);
                ma[x] = 1;
            }
        }
        sort(arr.begin(), arr.end());
        int l = 0, r = 0;
        int n = nums.size() - 1;
        int ans = 1000000;
        while (r < arr.size())
        {
            if (arr[r] - arr[l] <= n)
            {
                ans = min(ans, n - r + l);
                r += 1;
            }
            else l += 1;
        }
        return ans;
    }
};